/**
 * Created by xuxujunjunforever on 2015/12/9.
 */
'use strict'

let Fibers = require('fibers');

let models = require('../../../modules/models');
let errors = require('../../../config/errors');
let Counter = models.Counter;
let CounterDao = models.CounterDao;

let counterService = require('../../../modules/services/counter');

function setUp(callback) {
    new Fibers(() => {
        try {
            CounterDao.remove({});
            let counter = new Counter({
                name: 'test',
                count: 0
            });
            CounterDao.create(counter);

            callback();
        }
        catch (e) {
            console.log(e)
        }
    }).run();
}

function actionNextIndexSuccessful(test) {
    let count = counterService.nextIndex('test');
    test.equals(1, count);

    test.done();
}

function actionNextIndexNameNotExist(test) {
    test.throws(() => {
        let count = counterService.nextIndex('test2');
    }, errors.classes.CounterNameNotFound);

    test.done();
}

module.exports = {
    setUp,
    actionNextIndexSuccessful,
    actionNextIndexNameNotExist
};
